$(function () {
    $("#jqGrid").jqGrid({
        url: '../circle/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},

            {
                label: '会员', align: "center", name: 'userName', index: 'userName', width: 80
                , formatter: nameLink,
                editable: true
            },
            {label: '内容', align: "center", name: 'content', index: 'content', width: 80},
            {
                label: '创建时间', align: "center", name: 'createTime', index: 'createTime', width: 80,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            },
            {
                label: '类型', align: "center", name: 'type', index: 'type', width: 80,
                formatter: function (value) {
                    return transCircle(value);
                }
            }],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

function nameLink(cellvalue, options, rowObject) {
    return '<a href="javascript:void(0)" onclick="getinfo(' + rowObject.id + ')">' + rowObject.userName + '</a>';
}

function getinfo(id) {
    vm.showList = false;
    vm.showVideo = true;
    vm.showImg = true;
    vm.title = "修改";
    vm.res = null;
    vm.getInfo(id)
}

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        showVideo: true,
        showImg: true,
        title: null,
        res: null,
        apiCircle: {},
        resVos: "",
        billVos: "",
        ruleValidate: {},
        q: {
            content: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.apiCircle = {};
        },
        update: function (event) {
            let id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.showVideo = true;
            vm.showImg = true;
            vm.title = "修改";
            vm.res = null;
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.apiCircle.id == null ? "../circle/save" : "../circle/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.apiCircle),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../circle/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../circle/info/" + id, function (r) {
                vm.apiCircle = r.apiCircle;
                vm.res = r.res;
                vm.billVos = r.billVos;
                vm.resVos = r.resVos;

                if (r.apiCircle.type == 1) {
                    vm.showImg = false;
                }
                if (r.apiCircle.type == 2) {
                    vm.showVideo = false;

                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'content': vm.q.content},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});